package com.smartstate.iot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.dto.LogDateDto;
import com.smartstate.iot.entity.vo.GatewayInfoVo;
import com.smartstate.iot.vo.TopicUpVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface TopicLogMapper extends BaseMapper<TopicLog> {

    @Select("SELECT COUNT(*) FROM topic_log WHERE deviceId = #{deviceId}")
    Integer getLog(TopicUpVo.Content content);

    @Update("update topic_log set  logname = #{content.deviceName},deviceId =#{content.deviceId} ,type=#{topicLog.type},detail=#{topicLog.detail} where  deviceId = #{content.deviceId}")
    Boolean updateByDeviceId(TopicLog topicLog, TopicUpVo.Content content);

    @Select("select * from topic_log where date >=logDateVo.startingTime and date <=logDateVo.deadline")
    List<TopicLog> getLogDateTime(LogDateDto logDateVo);

//    @Select("SELECT COUNT(*) FROM topic_log x WHERE date = ( SELECT MAX(date) FROM topic_log y WHERE x.deviceId = y.deviceId ) AND deviceId IN ( SELECT deviceId FROM topic_log GROUP BY deviceId ) and type = '正常';")
//    Integer findDeviceTypeOK();
//
//    @Select("SELECT COUNT(*) FROM topic_log x WHERE date = ( SELECT MAX(date) FROM topic_log y WHERE x.deviceId = y.deviceId ) AND deviceId IN ( SELECT deviceId FROM topic_log GROUP BY deviceId ) and type = '异常';")
//    Integer findDeviceTypeNo();
//
//    @Select("SELECT COUNT(*) FROM topic_log x WHERE date = ( SELECT MAX(date) FROM topic_log y WHERE x.deviceId = y.deviceId ) AND deviceId IN ( SELECT deviceId FROM topic_log GROUP BY deviceId ) and type = '离线';")
//    Integer findDeviceTypeOFF();
//
//    @Select("SELECT COUNT(type) FROM topic_log x WHERE date = ( SELECT MAX(date) FROM topic_log y WHERE x.deviceId = y.deviceId ) AND deviceId IN ( SELECT deviceId FROM topic_log GROUP BY deviceId ) ;")
//    Integer findDeviceType();


    @Select("SELECT * FROM topic_log x WHERE date = ( SELECT MAX(date) FROM topic_log y WHERE x.logname = y.logname ) AND logname IN ( SELECT logname FROM topic_log GROUP BY logname ) AND type = '正常';")
    List<TopicLog> selectNormalList();

//    @Select("SELECT * FROM topic_log x WHERE date = ( SELECT MAX(date) FROM topic_log y WHERE x.deviceId = y.deviceId ) ;")
//    List<TopicLog> getAllBox();

    @Select("SELECT * FROM topic_config;")
    List<TopicLog> getAllBox();

    @Select("SELECT * FROM topic_config x WHERE timesTamps = ( SELECT MAX(timesTamps) FROM topic_config y WHERE x.deviceId = y.deviceId ) ;")
    List<TopicLog> getAllDevice();

    @Select("SELECT * FROM topic_log x WHERE date = ( SELECT MAX(date) FROM topic_log y WHERE x.deviceId = y.deviceId ) AND deviceId = #{deviceId}")
    TopicLog getUpLogType(String deviceId);

    /**
     * 根据deviceId 查询盒子最新时间的状态
     */
    @Select("SELECT type FROM topic_log WHERE deviceId = #{s} order by date desc limit 1")
    String getTypeSize(String s);

    @Select("select id,type,logname,date,detail,deviceId from topic_log where deviceId = #{deviceId} order by date DESC limit 1")
    TopicLog getTypeByDeviceId(String deviceId);

    @Select("SELECT topic_log.type FROM topic_log WHERE deviceId = #{deviceId} ORDER BY date DESC LIMIT 1")
    String getGatewayInfo(String deviceId);
}
